<?php
/**
 * Created by PhpStorm.
 * User: Hanson
 * Date: 2016/9/9
 * Time: 21:16
 */

namespace App\Handlers\Middleware;


use App\Jobs\WechatMessage;

class WechatMiddleware
{

    /**
     * 精准匹配
     *
     * @return array
     */
    protected static function exact()
    {
        return [
            'develop' => 'App\Handlers\Middleware\Develop',
            '开网密码' => 'App\Handlers\Middleware\Password',
        ];
    }

    /**
     * 模糊匹配
     *
     * @return array
     */
    protected static function fuzzy()
    {
        return [
            'App\Handlers\Middleware\Transit' => ['花7', '花10', '花30', '712a', '712'],
            'App\Handlers\Middleware\Password' => ['密码'],
        ];
    }

    public static function fire($message)
    {
        $events = self::exact();

        $blurs = self::fuzzy();

        if($message->MsgType === 'text'){

            dispatch(new WechatMessage($message));

            if(isset($events[$message->Content])){
                return call_user_func_array([$events[$message->Content], 'fire'], [$message]);
            }

            if($blurs){
                foreach ($blurs as $event => $blur) {

                    if($blur){
                        foreach ($blur as $keyword) {
                            if(str_contains(strtolower($message->Content), $keyword)){
                                return call_user_func_array([$event, 'fire'], [$message, $keyword]);
                            }
                        }
                    }

                }
            }
        }

        return null;
    }

}